package clf.wajl.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.io.File;
import java.math.BigDecimal;
import java.util.LinkedList;

import clf.wajl.Global;
import clf.wajl.R;
import clf.wajl.ui.AnswerPopMenu;
import clf.wajl.ui.PopMenu;
import clf.wajl.utils.ScreenTools;

/**
 * 解题
 * <p>
 * 1.获取屏幕宽高
 * 2.获取设置问题时屏幕的宽高
 * 3.计算出宽高比例用于计算答案的坐标值
 */
public class AnswerActivity extends BaseActivity implements View.OnTouchListener, View.OnClickListener, PopupMenu.OnMenuItemClickListener {

	private ImageView picIV;
	private TextView answerPromptTV;
	private String TAG = "clf.wajl.activity.AnswerActivity";

	private LinkedList<String> answerWHList = new LinkedList<>();
	private LinkedList<String> promptList = new LinkedList<>();
	private String askId;
	private AnswerPopMenu mMenu;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_answer);

		initWidget();
		initData();
	}

	private void initData() {

		Intent intent = getIntent();
		askId = intent.getStringExtra("askId");

		calculateAnswerHW();

		initPrompt();

	}

	private void initPrompt() {

		SharedPreferences sp = getSharedPreferences(Global.CONFIG_NAME, 0);
		String askPrompts = sp.getString(Global.ASK_PROMPT + askId, "点击下面的房顶&&点击上面的房顶&&点击下树叶");
		String[] askPromptArr = askPrompts.split("&&");
		for (int i = 0; i < askPromptArr.length; i++) {
			promptList.addLast(askPromptArr[i]);
		}

	}

	private void calculateAnswerHW() {

		SharedPreferences sp = getSharedPreferences(Global.CONFIG_NAME, 0);

		String askWH = sp.getString(Global.ASK_W_H + askId, "933&&1491@@42&&655@@260&&995");
		String askScreenWH = sp.getString(Global.ASK_SRCEEN_W_H + askId, "1080&1920");

		if (!TextUtils.isEmpty(askWH)) {
			String[] aswh = askScreenWH.split("&");
			int asw = Integer.valueOf(aswh[0]);
			int ash = Integer.valueOf(aswh[1]);

			int currentW = ScreenTools.instance(this).getScreenWidth();
			int currentH = ScreenTools.instance(this).getScreenHeight();
			BigDecimal currentHBD = new BigDecimal(currentH);
			BigDecimal currentWBD = new BigDecimal(currentW);
			BigDecimal asWBD = new BigDecimal(asw);
			BigDecimal asHBD = new BigDecimal(ash);

			String[] askWHArr = askWH.split("@@");
			for (int i = 0; i < askWHArr.length; i++) {
				String[] wh = askWHArr[i].split("&&");
				int aw = Integer.valueOf(wh[0]);
				int ah = Integer.valueOf(wh[1]);

				BigDecimal awBD = new BigDecimal(aw);
				BigDecimal ahBD = new BigDecimal(ah);

				if (asw != currentW) {

					BigDecimal scaleHBD = null;
					BigDecimal scaleWBD = null;
					if (currentH > ash) {
						scaleHBD = currentHBD.divide(asHBD, 2, BigDecimal.ROUND_DOWN).setScale(2);
						scaleWBD = currentWBD.divide(asWBD, 2, BigDecimal.ROUND_DOWN).setScale(2);
						int answerH = ahBD.multiply(scaleHBD).intValue();
						int answerW = awBD.multiply(scaleWBD).intValue();

						answerWHList.add(String.format("%s&&%s", answerW, answerH));

					} else {
						scaleHBD = asHBD.divide(currentHBD, 2, BigDecimal.ROUND_DOWN).setScale(2);
						scaleWBD = asWBD.divide(currentWBD, 2, BigDecimal.ROUND_DOWN).setScale(2);
						int answerH = ahBD.divide(scaleHBD, 2, BigDecimal.ROUND_DOWN).intValue();
						int answerW = awBD.divide(scaleWBD, 2, BigDecimal.ROUND_DOWN).intValue();
						answerWHList.add(String.format("%s&&%s", answerW, answerH));

					}

				} else {
					answerWHList.add(String.format("%s&&%s", aw, ah));

				}
			}


		}
	}

	private void initWidget() {
		picIV = findViewById(R.id.iv_answer_pic);
		answerPromptTV = findViewById(R.id.tv_title_right);

		picIV.setClickable(true);
		answerPromptTV.setClickable(true);

		picIV.setOnTouchListener(this);
		answerPromptTV.setOnClickListener(this);

		SharedPreferences sp = getSharedPreferences(Global.CONFIG_NAME, 0);
		String picUrl=sp.getString(Global.ASK_PIC_URL+askId,"");
		if (!TextUtils.isEmpty(picUrl)){

			File file=new File(picUrl);
			Glide.with(this).load(file).into(picIV);
		}

		initMenu();

	}


	private void initMenu() {
		mMenu = new AnswerPopMenu(this);
		mMenu.addItem("答案提示1", 0);
		mMenu.addItem("答案提示2", 1);
		mMenu.addItem("答案提示3", 2);
		mMenu.setOnItemSelectedListener(new PopMenu.OnItemSelectedListener() {
			@Override
			public void selected(View view, PopMenu.Item item, int position) {
				Toast.makeText(AnswerActivity.this, promptList.get(item.id), Toast.LENGTH_SHORT).show();
				mMenu.dismiss();
			}
		});
	}

	@Override
	public boolean onTouch(View view, MotionEvent motionEvent) {

		switch (motionEvent.getAction()) {
			case MotionEvent.ACTION_UP:

				int y = (int) motionEvent.getY();
				int x = (int) motionEvent.getX();

				findMatchPoint(y, x);

				break;
		}

		return false;
	}

	private void findMatchPoint(int y, int x) {

		for (int i = 0; i < answerWHList.size(); i++) {

			String awh = answerWHList.get(i);
			int w = Integer.parseInt(awh.split("&&")[0]);
			int h = Integer.parseInt(awh.split("&&")[1]);

			if (y < h + 20 && y > h - 20 && x < w + 20 && x > w - 20) {
				Toast.makeText(AnswerActivity.this, "恭喜你找到啦！", Toast.LENGTH_SHORT).show();
				return;
			} else {

			}
		}

		Toast.makeText(AnswerActivity.this, "年轻的少年哦~", Toast.LENGTH_SHORT).show();

		Log.i("onTouch", "=============y=" + y + "==============x=" + x);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.tv_title_right:
				mMenu.showAsDropDown(view);

				break;
		}
	}

	@Override
	public boolean onMenuItemClick(MenuItem menuItem) {
		switch (menuItem.getItemId()) {
			case R.id.tv_prompt_one:

				break;
			case R.id.tv_prompt_two:
				Toast.makeText(this, promptList.get(1), Toast.LENGTH_SHORT).show();
				break;
			case R.id.tv_prompt_three:
				Toast.makeText(this, promptList.get(2), Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
		}
		return false;
	}
}
